package cn.edu.nuc.ssm.service.impl;

import cn.edu.nuc.ssm.dao.CategoryMapper;
import cn.edu.nuc.ssm.dao.UserMapper;
import cn.edu.nuc.ssm.entity.Category;
import cn.edu.nuc.ssm.entity.User;
import cn.edu.nuc.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2017/4/6.
 */
@Service
class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public int regist(User user) {

      int i =   userMapper.insert(user);
        if (i<=0){

            return 0;
        }
        return 1;
    }

    @Override
    public int validUsername(String username) {

        List<User> list = userMapper.findByUsername(username);

        if (list==null || list.size()<=0){
            //通过校验
            return 1;
        }
        return 0;
    }

    @Override
    public User login(String username, String password) {

        List<User> list = userMapper.findByUsernameAndPassword(username, password);
        if (list==null || list.size()<=0){
            //通过校验
            return null;
        }
        return list.get(0);
    }

    @Override
    public User findbyId(int id) {

        return userMapper.findbyId(id);
    }

    @Override
    public int updateUserMessage(User user) {

        int i = userMapper.updateByPrimaryKeySelective(user);

        return i;
    }

    public List<Category> findbyzhiCategory(Integer uid){

        return categoryMapper.findbyCategoryBelongAndUid(1,uid);
    }

    public List<Category> findbyshouruCategory(Integer uid){

        return categoryMapper.findbyCategoryBelongAndUid(2,uid);
    }

    public int saveInsertCategory(Category category){

        return categoryMapper.insertSelective(category);
    }

    @Override
    public int updateCategoryName(String name,Integer id) {

        return categoryMapper.updateNameById(name,id);
    }

    @Override
    public List<User> findAll() {

        return userMapper.find();
    }

    @Override
    public void freeze(int id) {

        userMapper.updateStatus(id);
    }

    @Override
    public void defreeze(int id) {

        userMapper.updateStatus1(id);
    }

    @Override
    public void updatePic(User user) {

        userMapper.updatepicbyId(user.getPic(),user.getId());
    }

    @Override
    public String findpicByid(Integer id) {

      return  userMapper.findbyId(id).getPic();
    }
}
